package email

import (
	"bytes"
	"fmt"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"html/template"
	"log"
	"net/mail"
	"net/smtp"
	"strconv"
	"myblog/app/controller/system/siteconfig"
	"myblog/app/model"
	"myblog/app/utils/response"
	"github.com/scorredoira/email"
	"github.com/zhnxin/emailagent"
)

func TestEmail(r *ghttp.Request) {
	useremail := r.GetString("useremail")
	err := SendToEmail(useremail, "TestEmail", "该邮件为测试邮件！！！！")
	if err != nil {
		response.ErrorResp(r).SetBtype(model.Buniss_Edit).SetMsg(err.Error()).WriteJsonExit()
	}
	response.SucessResp(r).SetBtype(model.Buniss_Edit).SetData(g.Map{"code": 0, "msg": "发送成功"}).WriteJsonExit()
}

// 使用go自带的net/smtp包发送邮件
func SendToEmail11(useremail string, subject string, content string) error {
	//SMTP用户名
	mail_smtp_user := siteconfig.GetValueByName("mail_smtp_user")
	//发件人密码
	mail_smtp_pass := siteconfig.GetValueByName("mail_smtp_pass")
	//SMTP服务器
	mail_smtp_host := siteconfig.GetValueByName("mail_smtp_host")
	//SMTP端口
	mail_smtp_port := siteconfig.GetValueByName("mail_smtp_port")

	auth := smtp.PlainAuth("", mail_smtp_user, mail_smtp_pass, mail_smtp_host)
	to := []string{useremail}
	// 解析html模板文件
	t, err := template.ParseFiles("template/email.html")
	if err != nil {
		log.Fatalf("ParseFiles error: %v", err)
		return err
	}
	var body bytes.Buffer
	headers := "MIME-version: 1.0;\nContent-Type: text/html; charset=UTF-8;"
	body.Write([]byte(fmt.Sprintf("Subject: "+subject+"\n%s\n\n", headers)))

	//作为变量传递给html模板
	t.Execute(&body, struct {
		Content string
	}{
		Content: content,
	})
	err = smtp.SendMail(mail_smtp_host+":"+mail_smtp_port, auth, mail_smtp_user, to, body.Bytes())
	if err != nil {
		fmt.Printf("%v", err.Error())
		log.Fatalf("send mail error: %v", err.Error())
		return err
	}
	return nil
}

func generateEmail(useremail string, subject string, content string) (*email.Message, error) {
	//SMTP用户名
	mail_smtp_user := siteconfig.GetValueByName("mail_smtp_user")
	// compose the message
	t, err := template.ParseFiles("template/email.html")
	if err != nil {
		log.Fatalf("ParseFiles error: %v", err)
	}
	var body bytes.Buffer
	t.Execute(&body, g.Map{
		"content": content,
	})
	m := email.NewHTMLMessage(subject, body.String())
	//这里显示的发件人名和邮箱，随便写不会影响邮件发送
	m.From = mail.Address{Name: "admin", Address: mail_smtp_user}
	m.To = []string{useremail}

	// add attachments 添加附件
	//if err := m.Attach("agent.go"); err != nil {
	//	return nil, err
	//}

	// add headers
	m.AddHeader("X-CUSTOMER-id", "MIME-version: 1.0;\nContent-Type: text/html; charset=UTF-8;")
	return m, nil
}

func SendToEmail(useremail string, subject string, content string) error {
	//SMTP用户名
	mail_smtp_user := siteconfig.GetValueByName("mail_smtp_user")
	//发件人密码
	mail_smtp_pass := siteconfig.GetValueByName("mail_smtp_pass")
	//SMTP服务器
	mail_smtp_host := siteconfig.GetValueByName("mail_smtp_host")
	//SMTP端口
	mail_smtp_port, err := strconv.Atoi(siteconfig.GetValueByName("mail_smtp_port"))
	msg, err := generateEmail(useremail, subject, content)
	if err != nil {
		fmt.Printf("generateEmail%v", err)
		return err
	}
	agent := emailagent.New(mail_smtp_user, mail_smtp_pass, mail_smtp_host, mail_smtp_port, false)
	if mail_smtp_port == 465 { //ssl
		agent = emailagent.New(mail_smtp_user, mail_smtp_pass, mail_smtp_host, mail_smtp_port, true)
	}

	if err = agent.SendEmail(msg); err != nil {
		fmt.Printf("agent.SendEmail(msg)--%v", err)
	}
	return nil
}
